//
//  ByAddressVC.m
//  hutouben_ios
//
//  Created by mc on 16/11/29.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByAddressVC.h"
#import "ByAddressCell.h"
#import "ByAddAddressVC.h"

@interface ByAddressVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ByAddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收货地址";
    [self addLeftBackBarButtonItem];
    [self initTableView];
    [self fetchData];
}
#pragma mark ---获取数据
- (void)fetchData{
    ByAddressModel *model = [[ByAddressModel alloc] init];
    model.contactname = @"小花";
    model.mobile = @"15256567778";
    model.detailinfo = @"杭州江干区下沙智慧谷博也网络";
    model.province = @"浙江省";
    model.is_default = @"1";
    [self.dataArray addObject:model];
    
    ByAddressModel *model1 = [[ByAddressModel alloc] init];
    model1.contactname = @"小花";
    model1.mobile = @"15256567778";
    model1.detailinfo = @"杭州江干区下沙智慧谷博也网络";
    model1.is_default = @"0";
    model1.province = @"浙江省";

    [self.dataArray addObject:model1];
    
    ByAddressModel *model2 = [[ByAddressModel alloc] init];
    model2.contactname = @"小花";
    model2.mobile = @"15256567778";
    model2.detailinfo = @"杭州江干区下沙智慧谷博也网络";
    model2.province = @"浙江省";
    model2.is_default = @"0";

    [self.dataArray addObject:model2];
    
    [self.tableView reloadData];
    
}


#pragma mark ---getter
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

#pragma mark ---创建视图
-(void)initTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = HTB_BG_GRAY;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.view.backgroundColor = HTB_BG_GRAY;
}

#pragma mark ----UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count + 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    /**添加地址*/
    if (indexPath.section == 0) {
        static NSString *indentier = @"addCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.font = FONT(16);
            cell.textLabel.textColor = HTB_TEXT_COLOR1;
            cell.detailTextLabel.textColor = HTB_TEXT_COLOR2;
            cell.detailTextLabel.font = FONT(14);
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        UIImage *icon = [UIImage imageNamed:@"添加"];
        CGSize imageSize = CGSizeMake(15, 15);
        UIGraphicsBeginImageContextWithOptions(imageSize, NO,0.0);         CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);
        [icon drawInRect:imageRect];
        cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        cell.textLabel.text = @"新增收货地址";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"还可添加%ld个地址",10 - self.dataArray.count];
        return cell;

    }
    else{
        ByAddressCell *cell = [ByAddressCell cellWithTableView:tableView];
        ByAddressModel *model = self.dataArray[indexPath.section -1];
        cell.model = model;
        return cell;
    
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 50;
    }else
        return 80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (self.dataArray.count == 10) {
            [ByCoreToolAlert alertError:@"最多只可以添加10个地址" duration:2.0];
            return;
        }
        ByAddAddressVC *vc = [[ByAddAddressVC alloc] init];
        vc.type = 0;
        [self by_normalPush:vc hasNavi:YES];
    }
    
    else{
        ByAddressModel *model = self.dataArray[indexPath.section -1];
        ByAddAddressVC *vc = [[ByAddAddressVC alloc] init];
        vc.type = 1;
        vc.alertModel = model;
        [self by_normalPush:vc hasNavi:YES];
    }
}
/***左划删除*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
        return UITableViewCellEditingStyleNone;
    else {
        return UITableViewCellEditingStyleDelete;
    }
    
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定义
//        self.deleteIndexPath = indexPath;
//        ByNetUserModel *user = [ByCommonCache getUserInfo];
//        ByAddressModel *model = self.dataAry[indexPath.row];
//        model.uid = user.uid;
//        [self.viewModel removeAnAddress:model];
        NSLog(@"删除");
    }];
    return @[deleteRoWAction];//最后返回这俩个RowAction 的数组
}

@end
